<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
    </page-filter>
    <dialog-table
      ref="resourceTable"
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :row-key="rowKey"
      :checked-row-list="checkedRowList"
      :reserve-selection="true"
      :check-box="checkBox"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-commonName="scope">
        {{ scope.row.commonName }}
      </template>
    </dialog-table>
  </page-dialog>
</template>
<script setup name="selectRepairOrder">
import PageDialog from '../PageDialog/index'
import DialogTable from '../DialogTable/index'
import PageFilter from '../NewPageFilter/index'
import { getCustomizeSetting } from '@/api/customize'
import { getRepairOrderCustomizeListPage } from '@/api/repair'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getRepairOrderCustomizeListPage,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  searchList: {
    type: Array,
    default: () => {
      return []
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {}
    },
  },
  tableKey: {
    type: String,
    default:
      'repairRepairOrderList-com.slip.spd.entity.vo.repair.RepairOrderVO',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
})
let sortString = ref('')
// 过滤相关配置
const filterInfo = ref({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: sortString.value,
    queryMap: {
      // deptAuth: {
      //   asTbName: "repair_order",
      //   columnName: "repair_dept_id",
      //   queryType: "IN",
      //   queryCategory: "AND",
      //   value: "deptAuth",
      // },
      // type: {
      //   asTbName: "core_product_new",
      //   columnName: "`type`",
      //   queryType: "EQUALS",
      //   queryCategory: "AND",
      //   value: "2",
      // },
      status: {
        // asTbName: "core_product_new",
        // columnName: "`status`",
        queryType: 'EQUALS',
        queryCategory: 'AND',
        value: 2,
      },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择报修单',
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref([])
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )
      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )
      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item, index) => {
        props.searchList.forEach((element) => {
          if (item.value === element.value || item.asValue === element.value) {
            filterInfo.value.searchList[index] = {
              ...filterInfo.value.searchList[index],
              ...element,
            }
          }
        })
        filterInfo.value.searchList[index].event = search
      })

      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...filterInfo.value.query.queryMap,
        ...props.query,
      }
      filterInfo.value.query.queryParams = {
        ...filterInfo.value.query.queryParams,
        ...props.queryParams,
      }
      tableInfo.value.api = props.api
      getList()
    }
  })
}

let obj = {
  orderNo: {
    label: '单据号',
    value: 'orderNo',
    componentType: 'input',
    show: false,
    type: 'column',
    hidden: true,
  },
  assetName: {
    label: '设备名称',
    value: 'assetName',
    componentType: 'input',
    show: false,
    type: 'column',
    hidden: true,
  },
  statusValue: {
    label: '状态',
    value: 'statusValue',
    asValue: 'status',
    componentType: 'select-model',
    code: 'repair_order_status',
    show: false,
    hidden: true,
  },
}
tableInfo.value.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
filterInfo.value.searchList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...proxy.$fn.deepClone(obj),
})
getCustomize()
</script>

<style scoped lang="scss"></style>
